package cn.itsource.hrm.controller;

import cn.itsource.hrm.util.RedisUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

/**
 * @author chenyuqi
 * @version v1.0.0
 * @date 2020/5/22
 */
@RestController

public class RedisController {
    /*** 设置值 *
     *  @param key
     * @param value
     */
    @GetMapping("/set")
    public void set(@RequestParam("key")String key, @RequestParam("value")String value){
        RedisUtils.INSTANCE.set(key,value);
    }


    /*** 获取值 * @param key
     * @return
     */
    @GetMapping("/get")
    public String get(@RequestParam("key")String key){
        return RedisUtils.INSTANCE.get(key); }


    /**
     * 设置（过期时间）
     * @param key
     * @param value
     */
    @GetMapping("/setex")
    public void setex(@RequestParam("key")String key,@RequestParam("value")String value,@RequestParam("second") Integer second){
        Jedis jedis = null;
        try {
            jedis = RedisUtils.INSTANCE.getSource();
            jedis.setex(key,second,value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis!=null){
                jedis.close();
            }
        }

    }
}
